package com.hellogood.work.evaluate.client.customer;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.work.evaluate.shared.po.Customer;
import com.hellogood.work.evaluate.shared.services.CustomerService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class CustomerTab extends Tab {
	private ListGrid listGrid = null;
	private IButton modifyBtn = null;

	public CustomerTab() {
		initView();
	}

	public void initData() {
		CustomerService.Util.getInstance().getList(new ListAsyncCallback());
	}

	private void initView() {
		TextItem textItem = new TextItem("searchItem", "关键字");
		textItem.setTitleColSpan(2);
		IButton searchBtn = new IButton("查询");
		DynamicForm form = new DynamicForm();
		form.setWidth("200");
		form.setNumCols(4);
		form.setFields(textItem);
		IButton refreshBtn = new IButton("刷新");
		ToolStrip toolStrip1 = new ToolStrip();
		toolStrip1.setMembersMargin(20);
		toolStrip1.addMember(form);
		toolStrip1.addMember(searchBtn);
		toolStrip1.addMember(refreshBtn);

		ListGridField codeField = new ListGridField("custcode", "客户编码");
		ListGridField nameField = new ListGridField("custname", "客户名称");
		ListGridField productCodeField = new ListGridField("productCode", "产品编码");
		ListGridField productNameField = new ListGridField("productName", "产品名称");
		ListGridField addressField = new ListGridField("address", "地址");
		ListGridField phoneField = new ListGridField("phone", "电话");
		ListGridField serverIPField = new ListGridField("severip", "服务器IP");
		ListGridField domainField = new ListGridField("domain", "域名");
		ListGridField vpnField = new ListGridField("vpn", "是否有VPN");
		ListGridField contactField = new ListGridField("contact", "联系人");
		ListGridField remarkField = new ListGridField("remark", "备注");

		listGrid = new ListGrid();
		listGrid.setShowRowNumbers(true);
		listGrid.setFields(codeField, nameField, productCodeField, productNameField, addressField, phoneField, serverIPField, domainField, vpnField,
				contactField, remarkField);

		IButton addBtn = new IButton("新增");
		modifyBtn = new IButton("修改");
		modifyBtn.setDisabled(true);
		ToolStrip toolStrip2 = new ToolStrip();
		toolStrip2.setMembersMargin(20);
		toolStrip2.setMembers(addBtn, modifyBtn);

		VLayout layout = new VLayout();
		layout.setMembersMargin(5);
		layout.addMember(toolStrip1);
		layout.addMember(listGrid);
		layout.addMember(toolStrip2);
		setPane(layout);
		setTitle("客户列表");

		listGrid.addSelectionChangedHandler(new SelectionChangedHandler() {

			@Override
			public void onSelectionChanged(SelectionEvent event) {
				if (listGrid.getSelectedRecord() != null) {
					modifyBtn.setDisabled(false);
				} else {
					modifyBtn.setDisabled(true);
				}
			}
		});

		refreshBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				initData();
			}
		});

		addBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				ModifyWindow window = new ModifyWindow(CustomerTab.this, null, null, false);
				window.show();
				window.setSize("300", "400");
				window.centerInPage();
			}
		});

		modifyBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Record r = listGrid.getSelectedRecord();
				if (r != null) {
					ModifyWindow window = new ModifyWindow(CustomerTab.this, r.getAttribute("procode"), r.getAttribute("proname"), true);
					window.show();
					window.setSize("300", "400");
					window.centerInPage();
				}
			}
		});
	}

	private class ListAsyncCallback implements AsyncCallback<List<Customer>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Customer> result) {
			if (result != null) {
				Record[] records = new Record[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Record r = new Record();
					r.setAttribute("custcode", result.get(i).getCustomerNo());
					r.setAttribute("custname", result.get(i).getCustomerName());
					r.setAttribute("procode", result.get(i).getProductCode());
					r.setAttribute("proname", result.get(i).getProductName());
					r.setAttribute("address", result.get(i).getAddress());
					r.setAttribute("phone", result.get(i).getTelephone());
					r.setAttribute("serverip", result.get(i).getServerIP());
					r.setAttribute("domain", result.get(i).getDomain());
					r.setAttribute("vpn", result.get(i).isHasVPN() ? "有" : "无");
					r.setAttribute("contact", result.get(i).getContact());
					r.setAttribute("remark", result.get(i).getRemark());
					records[i] = r;
				}
				listGrid.setData(records);
			}
		}
	}
}
